<!-- 分类元数据 -->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.Search"
        placeholder="搜索名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <el-select
        v-model="listQuery.CategoryTypes"
        class="filter-item"
        placeholder="元数据类型"
        size="middle"
        filterable
        multiple
        style="width: 240px"
      >
        <el-option
          v-for="item in typeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        点击搜索
      </el-button>

      <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增分类</el-button>
    </div>

    <el-table v-loading="listLoading" :data="listData.List" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.ID }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.Name }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="类型" width="100">
        <template slot-scope="scope">
          {{ scope.row.CategoryTypeDisplay }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listData.Total > 0"
      :total="listData.Total"
      :page.sync="listQuery.Page"
      :limit.sync="listQuery.PageSize"
      @pagination="getListData"
    />

    <el-dialog :visible.sync="dialogVisible" :title="dialogType === 'edit' ? '编辑分类元数据' : '新增分类元数据'">
      <el-form :model="oneData" label-width="80px" label-position="left">
        <el-form-item label="名称">
          <el-input
            v-model="oneData.Name"
            type="textarea"
            placeholder="可一次输入多个, 一行一个"
            :rows="2"
            :autosize="{ minRows: dialogType == 'edit' ? 1: 2, maxRows: dialogType == 'edit' ? 1: 10 }"
            style="width: 240px"
            minlength="1"
            show-word-limit
/>
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="oneData.CategoryType"
              placeholder="类型"
              style="width: 200px"
              class="filter-item"
            >
          <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmSubmitDialog">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import * as data_api from '@/api/warehouse/category_meta'
import * as data_dict from '@/dict/category'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const defaultData = {
  ID: 0,
  Name: '',
  CategoryType: '',
}

export default {
  components: { Pagination },
  data() {
    return {
      listData: {
        List: [],
        Total: 0
      },
      listLoading: false,
      listQuery: {
        Page: 1,
        PageSize: 10,
        CategoryTypes: [],
        Search: '',
        Sorts: ''
      },
      oneData: Object.assign({}, defaultData),
      dialogVisible: false,
      dialogType: 'new',
    }
  },
  computed: {
    typeOptions() {
      return data_dict.CategoryAllTypes.map(v => ({
        label: data_dict.getCategoryTypeDisplayFull(v),
        value: v,
      }))
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    getListData() {
      this.listLoading = true
      data_api.list(this.listQuery).then(res => {
        this.listData = res.data
        this.normalizeData()
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },

    normalizeData() {
      for (const item of this.listData.List) {
        item.CategoryTypeDisplay = data_dict.getCategoryTypeDisplay(item.CategoryType)
      }
    },

    handleFilter() {
      this.listQuery.Page = 1
      this.getListData()
    },

    async confirmSubmitDialog() {
      const isEdit = this.dialogType === 'edit'
      console.log(this.oneData)

      if (!isEdit) {
        await data_api.create(this.oneData)
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: `新增分类: ${this.oneData.Name}(${data_dict.getCategoryTypeDisplay(this.oneData.CategoryType)}) 成功`
        })
      } else {
        await data_api.update(this.oneData)
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: 'Update succed!'
        })
      }
      this.getListData()
    },
    handleAdd() {
      this.oneData = Object.assign({}, defaultData)
      this.oneData.name
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.oneData = deepClone(scope.row)
    },

    handleDelete(scope) {
      const categoryStr = `${scope.row.Name}(${data_dict.getCategoryTypeDisplay(scope.row.CategoryType)})`
      this.$confirm(`确认删除分类: ${categoryStr}`, 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await data_api.delete_({
            ID: scope.row.ID
          })
          this.$message({
            type: 'success',
            message: `删除 ${categoryStr} 成功`
          })
          this.getListData()
        })
        .catch(err => { console.error(err) })

    }
  }
}
</script>

  <style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }
  }
</style>
